package com.changgou.seckill.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.dao.SeckillOrderMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import entity.SystemConstants;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.seckill.listener *
 * @Date 2019-12-12
 * @since 1.0
 */
@Component
@RabbitListener(queues = "queue.seckillorder")
public class SeckillOrderHandler {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    //监听发送过来的消息
    @RabbitHandler
    public void handlerMsg(String msg) {

        //1.获取消息本身 (就是所有的微信返回出来的数据)

        //2.转成MAP
        Map<String, String> map = JSON.parseObject(msg, Map.class);

        if (map != null) {
            if (map.get("return_code").equals("SUCCESS")) {
                String attach = map.get("attach");
                Map attachMap = JSON.parseObject(attach, Map.class);//attach中有:用户名
                attachMap.put("username","szitheima");//todo 写死用户名
                if (map.get("result_code").equals("SUCCESS")) {//支付成功
                    //3.从redis中获取 用户的预订单
                    //从attach中获取用户名

                    SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).get(attachMap.get("username"));
                    //4.将预订单存储到mysql中

                    //设置状态
                    seckillOrder.setStatus("1");//已经支付
                    //设置支付时间
                    String time_end = map.get("time_end");//支付时间
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    try {
                        Date date = simpleDateFormat.parse(time_end);
                        seckillOrder.setPayTime(date);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    //设置transaction_id
                    String transaction_id = map.get("transaction_id");
                    seckillOrder.setTransactionId(transaction_id);
                    seckillOrderMapper.insertSelective(seckillOrder);
                    //5.删除掉原来的redis中的预订单
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).delete(attachMap.get("username"));
                    //6.删除掉原来的排队的标识
                    redisTemplate.boundHashOps(SystemConstants.SECK_KILL_GOODS_COUNT_KEY).delete(attachMap.get("username"));

                } else {
                    //支付失败---->1.关闭微信的支付订单 2.恢复库存 3.删除预订单

                    //关闭微信订单:
                        //1. 在支付微服务中编写一个关闭微信订单的接口
                        //2. 编写一个feign 调用
                    //2.恢复库存
                    SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_USER_STATUS_KEY).get(attachMap.get("username"));
                    SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + seckillStatus.getTime()).get(seckillStatus.getGoodsId());

                    if (seckillGoods == null) {
                        //从数据库获取商品信息
                        seckillGoods = seckillGoodsMapper.selectByPrimaryKey(seckillStatus.getGoodsId());
                    }

                    seckillGoods.setStockCount(seckillGoods.getStockCount() + 1);
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + seckillStatus.getTime()).put(seckillStatus.getGoodsId(), seckillGoods);

                    //3.删除预订单
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).delete(attachMap.get("username"));
                    //4.删除排队标识
                    redisTemplate.boundHashOps(SystemConstants.SECK_KILL_GOODS_COUNT_KEY).delete(attachMap.get("username"));


                }
            }
        }

    }
}
